home *** CD-ROM | disk | FTP | other *** search
/ Software USA 3 #11 / Software USA Volume 3.11.iso / mac / Games / World Builder / Documentation / Variables Tutorial 1.2 < prev    next >
Text File  |  1995-11-27  |  18KB  |  278 lines

  1.        Understanding User Variables In World Builder
  2.  
  3.                          By Ray R. Dunakin III
  4. --------------------------------------------------
  5.  
  6. What is a variable? A variable is a word in the programming language that stands for something. Most of the special variables in World Builder stand for a specific, fixed thing. For instance, the variable PLAYER@ stands for the player character, regardless of what that character's name is. The variable LOOP# stands for the number of commands given in the current scene. It is a way of counting and storing a number, in this case the number of commands given by the player in a scene. It can't be used for anything else.
  7.  
  8. User variables stand for numbers. But with user variables, the programmer gets to decide what number is stored in the variable, and what it stands for. This gives the programmer tremendous freedom to control the events of the game.
  9.  
  10. A user variable is designated by a letter/number combination, using a single uppercase letter (A-Z) followed by a single digit (1-9). The standard numeric sign is also a part of the variable name. Here are some examples of user variables:
  11.  
  12. A1#      M7#     H3#    L9#     
  13.  
  14. These are just the names of the variables. You get to decide what number each name stands for. To make a variable stand for a specific number, use the LET statement:
  15.  
  16. LET{A1#=100}
  17.  
  18. In the above example, the variable A1# is assigned a value of 100. Numbers assigned to variables must be whole numbers in the range from -32768 to 32768. Do not use commas in the number.
  19.  
  20. You can change the number of a variable at any time.
  21.  
  22. "What good is this?" you might ask. Well, by using variables you can keep track of what the player has done during the game. Suppose you want the player to get new information everytime he talks to a certain character. How would you (or the game) know if this is the first time the player has spoken to that character, or the fifth time? Only by using a variable to keep track of it, like this:
  23.  
  24. IF{TEXT$=TALK}OR{TEXT$=INFO}OR{TEXT$=ASK}THEN
  25.     IF{M1#<1}THEN
  26.         PRINT{INNKEEPER: "I heard that some bandits hideout in the canyons to the north of town. Watch your step if you travel that way, stranger."}
  27.         LET{M1#=1}
  28.     EXIT
  29.     IF{M1#=1}THEN
  30.         PRINT{INNKEEPER: "Last winter an old man was in here talking about a treasure hidden in the castle ruins. No one's ever found it though."}
  31.         LET{M1#=2}
  32.     EXIT
  33.     IF{M1#=2}THEN
  34.         PRINT{INNKEEPER: "My grandfather was there the day the old king died. He said the king's last words were, 'Bite the wax tadpole.'"}
  35.         LET{M1#=3}
  36.     EXIT
  37. PRINT{INNKEEPER: "Sorry young fella, I haven't heard anything new lately."}
  38. EXIT
  39.  
  40. In this example, everytime the player enters TALK (or a similar command), the program checks to see what the variable M1# is set at. The first nested IF/THEN statement determines whether the variable is less than one. (Variables that haven't previously been set do not equal any number, including zero, so this is the best way to test for an unset variable.) 
  41.  
  42. If M1# is less than one, then the program prints the text giving the player the first message. A LET statement then updates the variable, making it equal one. The program then exits. 
  43.  
  44. The next time the player enters the TALK command, the program finds that M3# is not less than one, so it skips the first IF/THEN statement and moves on to the second one. This next statement tests to see if the variable equals one. Since the variable now equals one, the program displays the second message for the player, and again updates the variable to equal 2, and exits.
  45.  
  46. You can continue like this as many times as necessary, within the limits of the scene code capacity. At the end, there is a default message that will be automatically printed anytime the variable is not equal to a number previously handled.
  47.  
  48. That arrow-like symbol (<) is called a comparison operator. It means "less than". An arrow facing the other direction (>) means "greater than." Both symbols can also mean "does not equal" or "does not have," depending on how they are used. These two symbols can be used for other things besides numbers:
  49.  
  50. IF{KNIFE>PLAYER@}THEN
  51.  
  52. This statement tests whether the player has the knife. Literally, this statement says "If the knife does not equal the player, then..." In this case, "does not equal" means the knife is not in the player's possession. You can also use = to see if an object is in the player's possession, or in a scene, etc. Whether you use (>) or (=) will depend on the specific circumstances involved. Below are some examples of ways to use these comparison operators, and their meanings:
  53.  
  54. IF{MONSTER@=SCENE@}THEN      ("If any non-player character is in the     
  55.                                                   current scene....")
  56.  
  57. IF{SHOTGUN>THUG}THEN        ("If the character called 'thug' does not have the 
  58.                                                shotgun...")
  59.  
  60. IF{INNKEEPER=SCENE@}THEN     ("If the character or object called 'innkeeper' 
  61.                                                  is in the current scene...")
  62.  
  63. IF{LAMP>PLAYER@}THEN          ("If the player does not have the lamp...")
  64.  
  65.  
  66. IF{H2#<4}THEN        ("If variable H2# is less than four...")
  67.  
  68. IF{D7#>0}THEN       ("If variable D7# is more than zero...")
  69.  
  70. IF{B1#=20}AND{C1#<1}THEN       ("If variable B1 equals 20, AND variable C1 is 
  71.                                                     less than one [or unset]...)
  72.                                                     Notice that both conditions must be true.
  73.  
  74. IF{MOOSE=SCENE@}OR{SQUIRREL=SCENE@}THEN
  75.                                                    ("If the object or character called 'moose' is in
  76.                                                    the scene, OR the object or character called
  77.                                                    'squirrel' is in the scene...") 
  78.                                                    Notice that if either of these conditions is true, 
  79.                                                    the statement will be executed.
  80.  
  81. When handling commands, there are two more comparison operators: (==) and (>>). Using a pair of equals signs means that the command must match EXACTLY for the statement to be true. Using a pair of "greater than" signs means the statement is true only if the command does NOT match exactly. Remember, these are used only for text. Here are some examples:
  82.  
  83. IF{TEXT$==EAT FOOD}THEN
  84.  
  85. IF{TEXT$>>PASSWORD}THEN
  86.  
  87. In the first example, the player would have to enter "eat food" exactly like that. Anything else, such as "eat some food" would not register. There are not many ways in which this kind of command structure is useful. 
  88.  
  89. In the second example, the statement is true if the player enters anything other than "password" exactly that way. This is also something that has limited usefulness.
  90.  
  91. Anyway, let's get back to user variables. We've seen how a variable can be set to count the times that a player talks to another character, and how that can be used to control the character's replies. What other things are variables good for? Well, let's say you want the player to get "hungry" on a regular basis. At some point in the very beginning of the game, you'd need to set a variable to represent the player's apetite:
  92.  
  93. LET{H1#=95}
  94.  
  95. This gives the player a "full stomach" to start with. (It can be any number, not just 95.) From here you need a way of reducing that number as the player moves through the game. This will simulate the player's growing hunger with the passing of time. Since there is no clock in a WB game, the easiest way to simulate this is by subtracting one from the variable everytime the player enters a scene. So in each scene you'd need something like this at the top of the scene code:
  96.  
  97. IF{LOOP#=0}THEN
  98.     LET{H1#=H1#-1}
  99. END
  100.  
  101. With this code, the program will subtract one from the variable H1# when the player enters the scene. Once the player makes any move or command, the loop number increases beyond zero, and the statement is ignored. If the player leaves the scene, then returns, the loop is reset to zero, and one more point is subtracted from H1#. 
  102.  
  103. Now we have a way of counting down the variable that represents the player's need for food. But how do you put it to use? 
  104.  
  105. Since the code needed to tell the player when he's hungry is a bit longer, it's best to put it in the global code. At the top of the global code, you'd need something like this:
  106.  
  107. IF{LOOP#=0}THEN
  108.     IF{H1#<1}THEN
  109.         PRINT{You just died of starvation!}
  110.         MOVE{PLAYER@}TO{STORAGE@}
  111.     EXIT
  112.     IF{H1#<20}THEN
  113.         PRINT{You need to eat soon.}
  114.     END
  115. END
  116.  
  117. With this code, if the loop number is zero (the player just entered a scene), the program then finds out if variable H1 is less than one. If it is, it means the player has died of starvation. A message is displayed, and the player is moved to STORAGE@, ending the game. Since nothing more should happen, the statement is closed with EXIT.
  118.  
  119. If variable H1 is not less than one, then the next IF/THEN statement checks to see if the variable is less than 20. If it is any number below 20, a message is printed telling the player to find some food. Since the player is still alive, there's no need to stop there, so the statement closes with END. This allows the program to check the rest of the global code to see if anything else needs to happen.
  120.  
  121. Now we have a way of notifying the player when his "hunger" points are getting low, and a way to kill him when it reaches zero or less. Next we need a way for him to restore his hunger points by eating. If the player can carry food with him, the "eating" code should be in the Global code. If the player can only eat at a designated location, then you could put the "eat" code in that scene. In either case, here's what the code might look like:
  122.  
  123. IF{TEXT$=EAT}THEN
  124.     IF{TEXT$=FOOD}THEN
  125.         IF{FOOD=PLAYER@}THEN
  126.               SOUND{CHEWING}
  127.               MOVE{FOOD}TO{STORAGE@}
  128.               LET{H1#=95}
  129.               PRINT{The food satisfies your hunger.}
  130.          EXIT
  131.          PRINT{You don't have any food.}
  132.      EXIT
  133.      PRINT{Eat what?}
  134. EXIT
  135.  
  136. The first statement tests to see if the player has entered the word "eat". It could be just the word eat, or it could be part of an entire sentence. In either case, the statement would be true, and the program goes to the second statement, which tests to see if the word "food" was also entered. If the word "food" was not entered, then both the second and third statements are ignored, and the player is asked to specify what he wants to eat. 
  137.  
  138. If both words ("eat" and "food"), then the third statement tests to see if the player has the food in his possession. If this is true, then a chewing sound is played, the food is moved to STORAGE@, and the variable representing the player's hunger points is restored. 
  139.  
  140. If the player does not have the food, then the third statement is ignored, and a message is printed telling the player that he doesn't have the food. 
  141.  
  142. (Anytime you have one or more statements within another statement, it is called a nested statement.)
  143.  
  144. Now let's suppose you want the player to get more than one meal from his supply of food. For this you'd need another variable:
  145.  
  146. IF{TEXT$=EAT}THEN
  147.     IF{TEXT$=FOOD}THEN
  148.         IF{FOOD=PLAYER@}THEN
  149.               SOUND{CHEWING}
  150.               LET{H1#=95}
  151.               LET{F1#=F1#-1}
  152.               PRINT{The food satisfies your hunger.}
  153.               IF{F1#<1}THEN
  154.                    MOVE{FOOD}TO{STORAGE@}
  155.                    PRINT{You food has run out.}
  156.               EXIT
  157.          EXIT
  158.          PRINT{You don't have any food.}
  159.      EXIT
  160.      PRINT{Eat what?}
  161. EXIT
  162.  
  163. In this case, variable F1 is used to represent the amount of food the player has. Everytime the player eats, the variable is reduced by one. After executing the "eat" code, a fourth IF/THEN statement checks to see if the variable representing the food is less than one. If variable is less than one, then the food is moved to STORAGE@ and a message is displayed telling the player that his food has run out. 
  164.  
  165. Of course, when the player first gets the food, you'd have to set the variable F1# to a number representing the number of meals he can get from it. And after the food runs out, when he finds more food later (really the same "food" object), you'd have to reset the variable. 
  166.  
  167. ---------------------------------------------
  168.  
  169. In these examples, the variables representing the player's hunger, and the meals in his food supply, are first set, then gradually reduced. This works fine. However, you could easily do it the other way around: Instead of setting the variables and then reducing them, you could just add to the variables each time. In this way, you could skip the initial step of setting the variables. Then instead of killing the player when his hunger goes down to zero, you could kill him when it goes up to 95 (or whatever.) 
  170.  
  171. And instead of his food running out when the meals number goes down to zero, you could have it run out when the meals number goes up to 3 (or whatever).
  172.  
  173. ---------------------------------------------
  174.  
  175. Here's another way you can use variables. Suppose you want the player to use a computer, but only after a chip has been installed. Use a variable to find out whether or not the player has installed the chip:
  176.  
  177. IF{TEXT$=USE}THEN
  178.     IF{TEXT$=COMPUTER}THEN
  179.         IF{C1#=1}THEN
  180.              SOUND{BEEP}
  181.              MOVE{SCREEN.1}TO{SCENE@}
  182.              PRINT{The computer screen lights up, displaying a message:
  183.              PRINT{COMPUTER: "Greetings! Click the screen to continue."}
  184.         EXIT
  185.         PRINT{Nothing happens when you push the startup button on the computer.}
  186.     EXIT
  187.     IF{TEXT$=CHIP}THEN
  188.          IF{CHIP=PLAYER@}THEN
  189.               SOUND{CLICK}
  190.               MOVE{CHIP}TO{STORAGE@}
  191.               LET{C1#=1}
  192.               PRINT{The chip is now in the computer.}
  193.          EXIT
  194.          PRINT{You don't have the chip.}
  195.      EXIT
  196. END
  197.  
  198. The first statement here tests to see if the player has entered the word "use." If he has, then the second statement checks to see if he has also entered the word "computer." If the player has entered both words ("use" and "computer"), then the third statement tests to see if the value of variable C1 is one. If C1# equals one, it means the chip has been installed, and a sound is played, an object that depicts the computer's startup screen is moved to the scene, and a message is printed. 
  199.  
  200. If variable C1# does not equal one, then the third statement is ignored, and a message is printed telling the player that he doesn't have the chip.
  201.  
  202. If the player did not enter the word "computer," then the second statement is ignored. The program then continues down to the next nested statement, which checks to see if the player has entered the word "chip." If he has, then another statement checks to see if the player has the chip. 
  203.  
  204. If the player does have the chip, then a sound is played, the chip is moved to STORAGE@, the variable C1# is set to one, and a message is displayed telling the player that the chip has been installed. Now if the player enters the words "use" and "computer", the computer will start up.
  205.  
  206. --------------------------
  207. You could also have additional statements nested in the scene "use" code, to handle specific things the player might be likely to use, such as circuit that doesn't fit, a tool, a disk, etc.
  208. --------------------------
  209.  
  210. Notice that if the player enters the word "use", but has not entered "computer" or "chip," then the entire section of code closes with END. You should then have a statement in the global code that provides the default response for anything unusual that the player tries to use:
  211.  
  212. IF{TEXT$=USE}THEN
  213.     PRINT{That doesn't work.}
  214. EXIT
  215.  
  216. ----------------------------------------------
  217.  
  218. Until recently, I was unaware of any simple way to retrieve the number stored in a variable. In my earlier games, I used the following method:
  219.  
  220. For instance, if you want to find out how much money the player has whenever he types in a dollar sign. Let's say you are using variable M1# to represent the money the player has. As he goes through the game, he spends some and finds some. Adding and subtracting is easy, but finding out the total is a real problem. You can only find the value of the variable like this:
  221.  
  222. IF{TEXT$=$}THEN
  223.     IF{M1#<1}OR{CASH>PLAYER@}THEN
  224.          PRINT{You don't have any money.}
  225.     EXIT
  226.     IF{M1#=1}THEN
  227.          PRINT{You have $1.}
  228.     EXIT
  229.     IF{M1#=2}THEN
  230.          PRINT{You have $2.}
  231.     EXIT
  232.     IF{M1#=3}THEN
  233.          PRINT{You have $3.}
  234.     EXIT
  235.     IF{M1#=4}THEN
  236.          PRINT{You have $4.}
  237.     EXIT
  238.  
  239.  (...etc, etc, etc.)
  240.  
  241. As you can see, this is pretty tedious, and worse yet, it takes up a lot of precious code space. Remember, you only have a limited amount of room for code in each scene, and in the global code. If you want the player's cash to go as high as one hundred, you'd need one hundred IF/THEN statements to find out how much he has!! This is why I've limited the cash in my games to a maximum of $10. The player can only find or earn more money if his cash reserves fall below a certain amount. 
  242.  
  243. ------------
  244. UPDATE!
  245. ------------
  246. Shortly after I released version 1.0 of this tutorial, I was informed that there IS a simple way to print the value of a variable. For some reason this information was not in the official World Builder manual. Just use the PRINT feature, and the variable name, like this:
  247.  
  248. PRINT{M1#}
  249.  
  250. Unfortunately, this too has a slight limitation, since it can only print out the number. To make it part of a sentence, you have to do this:
  251.  
  252. IF{TEXT$=$}THEN
  253.     IF{M1#<1}OR{CASH>PLAYER@}THEN
  254.          PRINT{You don't have any cash.}
  255.     EXIT
  256.     PRINT{You have}
  257.     PRINT{M1#}
  258.     PRINT{dollars.}
  259. EXIT
  260.  
  261. When the player enters a dollar sign, this is what will be printed in the text window:
  262.  
  263. You have
  264. 470                                         (or whatever the current amount is)
  265. dollars.
  266.  
  267.  
  268. A big thanks to Bubrick@aol.com, aka Steven Merrill for telling me about this method of printing out the value of a variable!
  269. --------------------------------------------------
  270.  
  271. If you have any questions about World Builder programming, send email to RayDunakin@aol.com
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.